export default {
  //计算两个日期之间的时间间隔
  //date1:开始日期，date2结束日期
  //返回值：{d:天数,h:小时数,m:分钟数,s:秒数}
  calculateDateInterval(date1, date2) {
    let date1Time = date1.getTime()
    let date2Time = date2.getTime()
    let time = date2Time - date1Time
    let d = Math.floor(time / (24 * 60 * 60 * 1000))
    let h = Math.floor((time % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000))
    let m = Math.floor((time % (60 * 60 * 1000)) / (60 * 1000))
    let s = Math.floor((time % (60 * 1000)) / 1000)
    return {
      d: d,
      h: h,
      m: m,
      s: s,
    }
  },
  //计算两个日期之间的间隔的天数
  //date1:开始日期，date2结束日期，返回值：间隔的天数
  calculateDateIntervalDays(date1, date2) {
    let date1Time = date1.getTime()
    let date2Time = date2.getTime()
    let time = date2Time - date1Time
    return Math.floor(time / (24 * 60 * 60 * 1000))
  },
  //计算一个日期是否晚于另一个日期
  //date1:一个日期，date2:另一个日期
  //返回值：true:晚于，false:早于或相等
  isDateAfter(date1, date2) {
    return date1.getTime() > date2.getTime()
  },
  //传入一个日期对象，formatter格式化字符串，返回格式化后的日期字符串，例如："yyyy-MM-dd HH:mm:ss"
  formatDate(date, formatter = "yyyy-MM-dd HH:mm:ss") {
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour24 = date.getHours()
    let hour12 = hour24 > 12 ? hour24 - 12 : hour24
    let minute = date.getMinutes()
    let second = date.getSeconds()
    let millisecond = date.getMilliseconds()
    let week = date.getDay()
    let weekText = "星期" + "日一二三四五六".charAt(week)
    let o = {
      "y+": year,
      "M+": month,
      "d+": day,
      "H+": hour24,
      "h+": hour12,
      "m+": minute,
      "s+": second,
      "S+": millisecond,
      "w+": weekText,
    }
    for (let key in o) {
      let reg = new RegExp("(" + key + ")")
      if (reg.test(formatter)) {
        let group = RegExp.$1
        let value = o[key]
        if (group.length === 1) {
          formatter = formatter.replace(group, value)
        } else {
          let pad = group.length - (value + "").length
          let zero = ""
          for (let i = 0; i < pad; i++) {
            zero += "0"
          }
          formatter = formatter.replace(group, zero + value)
        }
      }
    }
    return formatter
  },
  //返回从当前时间开始，往前倒退20年的年份
  getPrevious20Years() {
    let date = new Date()
    let year = date.getFullYear()
    let years = []
    for (let i = year; i >= year - 20; i--) {
      years.push(i)
    }
    return years
  },
  //传入数字类型的秒数，返回 mm:ss 格式的时间字符串
  formatTime(seconds) {
    let m = Math.floor(seconds / 60)
    let s = Math.floor(seconds % 60)
    return `${m < 10 ? "0" + m : m}:${s < 10 ? "0" + s : s}`
  },
  //传入秒数，返回一个对象，包含 m 分、s 秒
  formatTimeObject(seconds) {
    let m = Math.floor(seconds / 60)
    let s = Math.floor(seconds % 60)
    return {
      m: m,
      s: s,
    }
  },
  //传入秒数，返回一个对象，包含时、分、秒
  formatTimeObject2(seconds) {
    let h = Math.floor(seconds / 3600)
    let m = Math.floor((seconds % 3600) / 60)
    let s = Math.floor(seconds % 60)
    return {
      h: h,
      m: m,
      s: s,
    }
  },
  //传入秒数，返回 X 时 X 分 X 秒 格式的时间字符串，如果某个时间单位为0，则不显示
  formatTimeChinese(seconds) {
    if (seconds <= 0) {
      return "0 秒"
    }
    let h = Math.floor(seconds / 3600)
    let m = Math.floor((seconds % 3600) / 60)
    let s = Math.floor(seconds % 60)
    let time = ""
    if (h > 0) {
      time += h + " 时 "
    }
    if (m > 0) {
      time += m + " 分 "
    }
    if (s > 0) {
      time += s + " 秒 "
    }
    return time
  },
  // 处理moment对象的日期专用的方法
  moment:{
    //计算两个日期之间间隔的天数，传入moment对象，返回间隔的天数
    calculateDateIntervalDays(date1, date2) {
      let time = date2.diff(date1)
      return Math.floor(time / (24 * 60 * 60 * 1000))
    },
    //计算两个日期之间间隔的天数、星期数、月数、年数，传入moment对象，返回一个对象
    calculateDateInterval(date1, date2) {
      let time = date2.diff(date1)
      let d = Math.floor(time / (24 * 60 * 60 * 1000))
      let w = Math.floor(d / 7)
      let m = date2.diff(date1, "months")
      let y = date2.diff(date1, "years")
      return {
        d: d,
        w: w,
        m: m,
        y: y,
      }
    },
  }
}
